package com.wkhyapp.lm.view;


import android.graphics.Bitmap;
import android.os.Build;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.maning.mndialoglibrary.MProgressDialog;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.base.BaseActivity;
import com.wkhyapp.lm.utils.StatusBarFontUtil;

import butterknife.BindView;

public class WebViewActivity extends BaseActivity {


    @BindView(R.id.close_tv)
    TextView close_tv;
    @BindView(R.id.title_tv)
    TextView title_tv;
    @BindView(R.id.back_iv)
    ImageView back_iv;
    @BindView(R.id.webView)
    WebView webview;
    private String url,title;
    @Override
    public void initUI() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            translucentStatusBar();
        }

        url = getIntent().getStringExtra("0");
        title = getIntent().getStringExtra("1");

        StatusBarFontUtil.setLightStatusBarColor(this);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(android.webkit.WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
            }
            @Override
            public void onReceivedTitle(android.webkit.WebView view, String title) {
                super.onReceivedTitle(view, title);
            }

        });

        webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) {
                return super.shouldOverrideUrlLoading(view, url);

            }
            @Override
            public void onPageFinished(android.webkit.WebView view, String url) {
                MProgressDialog.dismissProgress();

            }

            @Override
            public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                MProgressDialog.showProgress(mContext);
            }
        });
        title_tv.setText(title);
        webview.loadUrl(url);
    }

    @Override
    protected void initListener() {
        close_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        back_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (webview.canGoBack()) {
                    webview.goBack(); //goBack()表示返回WebView的上一页面
                }else {
                    finish();
                }
            }
        });
    }

    @Override
    protected void afterCreate() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_web_view;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) ) {
            if (webview.canGoBack()) {
                webview.goBack(); //goBack()表示返回WebView的上一页面
                return true;
            }else {
                finish();
                return true;
            }
        }
        return false;
    }

}
